package cwinsor.com.leaf_raking;

import java.util.Arrays;
import java.util.List;

public final class Day {

	/*
	 * properties
	 */
	private String name;

	/*
	 * getters
	 */
	public final String name() { return name; }

	/*
	 * private constructor
	 */
	private Day(String n) { name = n; }

	/*
	 * global instances
	 */
	public static final Day NOV_1 = new Day("11/1/14");
	public static final Day NOV_2 = new Day("11/2/14");
	public static final Day NOV_8 = new Day("11/8/14");
	public static final Day NOV_9 = new Day("11/9/14");
	public static final Day NOV_15 = new Day("11/15/14");
	public static final Day NOV_16 = new Day("11/16/14");

	/*
	 * global instances as a list (public method)
	 */
	public static final List<Day> asList() {
		return instanceList;		
	}

	/*
	 * instances list (private)
	 */
	private static final List<Day> instanceList = Arrays.asList(
			NOV_1,
			NOV_2,
			NOV_8,
			NOV_9,
			NOV_15,
			NOV_16);

	/*
	 * fromString
	 * converts string to StartTime
	 * fails if it finds zero or more than one
	 */
	public static Day fromString(String string) {
		Integer count = 0;
		Day theRtn = null;

		if (string==null) {
			Msg.fatal(dS + ": input string is null");
		}

		// search - match a longer string first!
		for (int i=Day.asList().size()-1; i>=0; i--) {
			Day day = Day.asList().get(i);
			if (string.contains(day.name())) {
				theRtn = day;
				count++;
			}
		}
		// sanity check - there should be exactly one
		if (!count.equals(1)) {
			Msg.fatal(dS + ": expected exactly one but there were " + count + " string was " + string);
		}
		return theRtn;
	}


	// debug
	private static String dS = "Day";
}